package cn.edu.cug.cs.gtl.apis.ui.tableuis;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.UIApp;
import cn.edu.cug.cs.gtl.apis.ui.table.TablePanel;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class TableUIApp extends UIApp {
    TablePanel tablePanel = new TablePanel();

    public TableUIApp(AbstractApplication theApp) {
        super(theApp);
    }

    @Override
    public JComponent initializeUI() {
        Object[] colNames = new String[10];
        Object[][] data = new String[10][10];

        for (int i = 0; i < 10; ++i) {
            colNames[i] = "Column" + i;

            for (int j = 0; j < 10; ++j) {
                data[i][j] = "(" + i + "," + j + ")";
            }
        }

        tablePanel.initial(colNames, data);
        return tablePanel;
    }

    public JComponent redrawCsvUI(FSDataInputStream fis) throws IOException {
        List<String[]> data = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        String line;
        while ((line = reader.readLine()) != null) {
            data.add(line.split(","));
        }
        reader.close();

        String[] columnNames = data.get(0);
        String[][] rowData = data.subList(1, data.size()).toArray(new String[0][]);

        tablePanel.removeAll();
        tablePanel.initial(columnNames, rowData);

        //重绘UI
        tablePanel.revalidate();
        tablePanel.repaint();

        return tablePanel;
    }

    public JComponent redrawXlsxUI(FSDataInputStream fis, String fileType) throws IOException {
        List<String[]> data = new ArrayList<>();

        Workbook workbook = null;
        // 去掉文件类型前面的点，以确保文件类型字符串正确
        if (fileType.startsWith(".")) {
            fileType = fileType.substring(1);
        }
        //根据文件类型选择适当的Workbook实现
        if ("xls".equalsIgnoreCase(fileType)) {
            workbook = new HSSFWorkbook(fis);
        } else if ("xlsx".equalsIgnoreCase(fileType)) {
            workbook = new XSSFWorkbook(fis);
        } else {
            throw new IllegalArgumentException("Unsupported file type: " + fileType);
        }

        Sheet sheet = workbook.getSheetAt(0);
        for (Row row : sheet) {
            List<String> rowData = new ArrayList<>();
            for (Cell cell : row) {
                switch (cell.getCellType()) {
                    case STRING:
                        rowData.add(cell.getStringCellValue());
                        break;
                    case NUMERIC:
                        rowData.add(String.valueOf(cell.getNumericCellValue()));
                        break;
                    default:
                        rowData.add("");
                        break;
                }
            }
            data.add(rowData.toArray(new String[0]));
        }
        String[] columnNames = data.get(0);
        String[][] rowData = data.subList(1, data.size()).toArray(new String[0][]);

        tablePanel.removeAll();
        tablePanel.initial(columnNames, rowData);

        //重绘UI
        tablePanel.revalidate();
        tablePanel.repaint();
        return tablePanel;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public Type getType() {
        return Type.TABLE_UI_APP;
    }
}
